Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

config-cache

Package Overview
Dependencies
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

config-cache

General purpose JavaScript object storage methods.

  • 1.3.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

config-cache NPM version Build Status

General purpose JavaScript object storage methods.

Install with npm

npm i config-cache --save

Usage

var Config = require('config-cache');
var config = new Config();

API

Cache

Initialize a new Cache

  • obj {Object}: Optionally pass an object to initialize with.
var cache = new Cache();

.set

Assign value to key or return the value of key.

  • key {String}
  • value {*}
  • expand {Boolean}: Resolve template strings with expander
  • returns {Object} Cache: to enable chaining
cache.set(key, value);

.get

Return the stored value of key. If the value does not exist on the cache, you may pass true as a second parameter to tell [set-object] to initialize the value as an empty object.

  • key {*}
  • create {Boolean}
  • returns: {*}
cache.set('foo', 'bar');
cache.get('foo');
// => "bar"

// also takes an array or list of property paths
cache.set({data: {name: 'Jon'}})
cache.get('data', 'name');
//=> 'Jon'

.constant

Set a constant on the cache.

  • key {String}
  • value {*}

Example

cache.constant('site.title', 'Foo');

.exists

Return true if key exists in cache. Dot notation may be used for nested properties.

  • key {String}
  • returns: {Boolean}

Example

cache.exists('author.name');
//=> true

.has

Return true if property exists and has a non-null value. Dot notation may be used for nested properties.

  • property {String}
  • returns: {Boolean}

Example

cache.has('author.name');
//=> true

.union

Add values to an array on the cache. This method is chainable.

  • returns {Object} Cache: to enable chaining

Example

// config.cache['foo'] => ['a.hbs', 'b.hbs']
cache
  .union('foo', ['b.hbs', 'c.hbs'], ['d.hbs']);
  .union('foo', ['e.hbs', 'f.hbs']);

// config.cache['foo'] => ['a.hbs', 'b.hbs', 'c.hbs', 'd.hbs', 'e.hbs', 'f.hbs']

.extend

Extend the cache with the given object. This method is chainable.

  • returns {Object} Cache: to enable chaining

Example

cache
  .extend({foo: 'bar'}, {baz: 'quux'});
  .extend({fez: 'bang'});

Or define the property to extend:

cache
  // extend `cache.a`
  .extend('a', {foo: 'bar'}, {baz: 'quux'})
  // extend `cache.b`
  .extend('b', {fez: 'bang'})
  // extend `cache.a.b.c`
  .extend('a.b.c', {fez: 'bang'});

.keys

Return the keys on this.cache.

  • returns: {Boolean}
cache.keys();

.hasOwn

Return true if key is an own, enumerable property of this.cache or the given obj.

  • key {String}
  • obj {Object}: Optionally pass an object to check.
  • returns: {Boolean}
cache.hasOwn([key]);

.clone

Clone the given obj or cache.

  • obj {Object}: Optionally pass an object to clone.
  • returns: {Boolean}
cache.clone();

.methods

Return methods on this.cache or the given obj.

  • obj {Object}
  • returns: {Array}
cache.methods('foo')
//=> ['set', 'get', 'enable', ...]

Data methods

Methods for reading data files, processing template strings and extending the cache.data object.

.process

Use expander to recursively expand template strings into their resolved values.

  • lookup {*}: Any value to process, usually strings with a cache template, like <%= foo %> or ${foo}.
  • opts {*}: Options to pass to Lo-Dash _.template.

Example

cache.process({a: '<%= b %>', b: 'c'});
//=> {a: 'c', b: 'c'}

.extendData

Extend the cache.data object with the given data. This method is chainable.

  • returns {Object} Cache: to enable chaining

Example

cache
  .extendData({foo: 'bar'}, {baz: 'quux'});
  .extendData({fez: 'bang'});

.plasma

Extend the data object with the value returned by plasma.

  • data {Object|String|Array}: File path(s), glob pattern, or object of data.
  • options {Object}: Options to pass to plasma.

Example:

cache
  .plasma({foo: 'bar'}, {baz: 'quux'});
  .plasma({fez: 'bang'});

See the plasma documentation for all available options.

.data

Extend the cache.data object with data from a JSON or YAML file, or by passing an object directly - glob patterns or file paths may be used.

  • values {Object|Array|String}: Values to pass to plasma.
  • process {Boolean}: If true,
  • returns {Object} Cache: to enable chaining
cache
  .data({a: 'b'})
  .data({c: 'd'});

console.log(config.cache);
//=> {data: {a: 'b', c: 'd'}}

When true is passed as the last argumemnt data will be processed by expander before extending cache.data.

cache.data({a: '<%= b %>', b: 'z'})
//=> {data: {a: 'z', b: 'z'}}

Clearing the cache

Methods for clearing the cache, removing or reseting specific values on the cache.

.omit

Omit properties from the cache.

  • returns {Object} Cache: to enable chaining

Example:

cache
  .omit('foo');
  .omit('foo', 'bar');
  .omit(['foo']);
  .omit(['foo', 'bar']);

.clear

Remove key from the cache, or if no value is specified the entire cache is reset.

Example:

cache.clear();

Usage Examples

.set

If expand: true is defined on the options, the value will be set using expander.

Examples:

// as a key-value pair
cache.set('a', {b: 'c'});

// or as an object
cache.set({a: {b: 'c'}});

// chaining is possible
cache
  .set({a: {b: 'c'}})
  .set('d', 'e');

Expand template strings with expander:

cache.set('a', {b: '${c}', c: 'd'}, true);

Visit the expander docs for more info.

  • option-cache: Simple API for managing options in JavaScript applications.
  • map-cache: Basic cache object for storing key-value pairs.
  • cache-base: Generic object cache for node.js/javascript projects.
  • config-cache: General purpose JavaScript object storage methods.
  • engine-cache: express.js inspired template-engine manager.
  • loader-cache: Register loader functions that dynamically read, parse or otherwise transform file contents when the name of the loader matches a file extension. You can also compose loaders from other loaders.
  • parser-cache: Cache and load parsers, similiar to consolidate.js engines.
  • helper-cache: Easily register and get helper functions to be passed to any template engine or node.js application. Methods for both sync and async helpers.

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Running tests

Install dev dependencies:

npm i -d && npm test

Author

Jon Schlinkert

License

Copyright (c) 2015 Jon Schlinkert
Released under the MIT license


This file was generated by verb-cli on April 02, 2015.

Keywords

FAQs

Package last updated on 02 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc